package com.yunfei.service;

import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.ResultStatus;
import com.yunfei.fegin.RedisService;
import com.yunfei.utils.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;

/**
 * @program: springclould_ylcaifu_App
 * @description:
 * @author: zhengyunfei
 * @create: 2019-01-21 13:13
 **/
@Service
public class ValidTokenService {
    @Autowired
    private RedisService redisService;
    //校验token是否有效
    public  boolean validToken(String token, ResultModel resultModel) {
        Object a=redisService.get("a");
        Object tokenObj=redisService.get(token);
        if(StringUtil.isNullOrEmpty(token)){
            //token非空校验
            resultModel.setCode(ResultStatus.TOKEN_NOTNULL.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_NOTNULL.getMessage());
            return true;
        }else{
            //参数校验成功
            String key=token;
            boolean obj=redisService.exists(key);
            if(!obj){
                String callBack=redisService.get("HystricCallBack");
                if(!StringUtil.isNullOrEmpty(callBack)){
                    //token非法
                    resultModel.setCode(-1);
                    resultModel.setMessage(callBack+"服务没有启动");
                    return true;
                }else{
                    //token非法
                    String value=redisService.get(key);
                    if(StringUtil.isNullOrEmpty(value)){
                        resultModel.setCode(ResultStatus.TOKEN_ERROR.getCode());
                        resultModel.setMessage(ResultStatus.TOKEN_ERROR.getMessage());
                        return true;
                    }

                }

            }
        }
        return false;
    }
}
